const { Dept } = require("../../models");
const { Op } = require("sequelize");
const { success, failure } = require("../../utils/responses");
const { paging } = require("../../utils/utils");
const { NotFoundError } = require("../../utils/errors");

/**
 * 分页
 * @param req
 * @param res
 */
exports.list = async (req, res) => {
  // 查询所有数据
  try {
    // 获取分页参数
    const { query, currentPage, pageSize, offset } = paging(req);
    // 排序+分页
    const condition = {
      order: [["id", "ASC"]],
      limit: pageSize,
      offset: offset,
    };
    // 模糊查询
    if (query.name) {
      condition.where = {
        name: {
          [Op.like]: `%${query.name}%`,
        },
      };
    }
    condition.where = {
      parentId: {
        [Op.eq]: query.parentId || "0", //根元素
      },
    };
    // 分页查询
    const { count, rows } = await Dept.findAndCountAll(condition);
    // 返回
    success(res, "查询成功", {
      lists: rows,
      pagination: {
        total: count,
        currentPage,
        pageSize,
      },
    });
  } catch (error) {
    failure(res, error);
  }
};

/**
 * 所有列表
 * @param req
 * @param res
 */
exports.all = async (req, res) => {
  try {
    const { query } = paging(req);
    // 排序
    const condition = {
      raw: true, //只返回数据的参数 --默认返回包装类
    };
    // 模糊查询
    if (query.name) {
      condition.where = {
        name: {
          [Op.like]: `%${query.name}%`,
        },
      };
    }
    const rows = await Dept.findAll(condition);
    // 返回
    success(res, "查询成功", rows);
  } catch (error) {
    failure(res, error);
  }
};

/**
 *
 * 详情
 * @param req
 * @param res
 */
exports.details = async (req, res) => {
  try {
    const data = await getById(req);
    success(res, "查询成功", data);
  } catch (error) {
    failure(res, error);
  }
};

/**
 * 查询
 * @param req
 * @param res
 */
exports.add = async (req, res) => {
  try {
    const body = filterBody(req);
    if (!body.parentId) {
      body.parentId = 0;
    }
    const data = await Dept.create(body);
    success(res, "创建成功", data, 201);
  } catch (error) {
    failure(res, error);
  }
};

/**
 * 更新
 * @param req
 * @param res
 */
exports.update = async (req, res) => {
  try {
    const data = await getById(req);
    const body = filterBody(req);
    await data.update(body);
    success(res, "更新成功", data);
  } catch (error) {
    failure(res, error);
  }
};

/**
 * 删除
 * @param req
 * @param res
 */
exports.del = async (req, res) => {
  try {
    const data = await getById(req);
    await data.destroy();
    success(res, "删除成功");
  } catch (error) {
    failure(res, error);
  }
};

/**
 * 获取当前
 * @param req
 */
async function getById(req) {
  const id = req.body.id || req.params.id || req.query.id;
  if (!id) {
    throw new NotFoundError(`ID: 参数错误`);
  }
  // 获取当前
  const data = await Dept.findByPk(id);
  if (!data) {
    throw new NotFoundError(`ID: ${id} not found`);
  }
  return data;
}

/**
 * 公共方法过滤白名单
 * @param req
 * @returns {*}
 */
function filterBody(req) {
  return {
    name: req.body.name,
    parentId: req.body.parentId,
    sort: req.body.sort,
    phone: req.body.phone,
    principal: req.body.principal,
    email: req.body.email,
    status: req.body.status,
    type: req.body.type,
    remark: req.body.remark,
  };
}
